package com.shd.edanpei.photo;

import android.content.Context;
import android.graphics.Bitmap;
import android.support.annotation.NonNull;
import android.support.v4.view.PagerAdapter;
import android.view.View;
import android.view.ViewGroup;

import com.bumptech.glide.Glide;
import com.shd.edanpei.R;

import java.util.ArrayList;

import uk.co.senab.photoview.PhotoView;
import uk.co.senab.photoview.PhotoViewAttacher;

/**
 * Created by mc on 2018/12/7.
 * yang-wen-wen
 */

public class PhotoViewAdapter extends PagerAdapter {

    private Context mContext;
    private ArrayList<String> mPhotoList;

    private View mView;
    private PhotoView mPhotoView;
    private OnClickListener mOnClickListener;

    public PhotoViewAdapter(Context context, ArrayList<String> photoList) {
        this.mContext = context;
        this.mPhotoList = photoList;
    }

    @Override
    public int getCount() {
        return mPhotoList == null ? 0 : mPhotoList.size();
    }

    @Override
    public boolean isViewFromObject(@NonNull View view, @NonNull Object object) {
        return view == object;
    }

    @NonNull
    @Override
    public Object instantiateItem(@NonNull ViewGroup container, int position) {
        mView = View.inflate(mContext, R.layout.item_photo_view, null);
        mPhotoView = mView.findViewById(R.id.photoView);
        Glide.with(mContext)
                .load(mPhotoList.get(position))
                .into(mPhotoView);

        mPhotoView.setOnPhotoTapListener(new PhotoViewAttacher.OnPhotoTapListener() {
            @Override
            public void onPhotoTap(View view, float v, float v1) {
                if (mOnClickListener != null){
                    mOnClickListener.onClick();
                }
            }
        });
        mPhotoView.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                if (mOnClickListener != null) {
                    Bitmap bitmap = mPhotoView.getDrawingCache();
                    if (bitmap != null) {
                        mOnClickListener.onLongClick(bitmap);
                    }
                }
                return true;
            }
        });
        container.addView(mView);
        return mView;
    }

    @Override
    public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
        container.removeView((View) object);

    }

    @Override
    public int getItemPosition(@NonNull Object object) {
        return POSITION_NONE;
    }

    public void setOnClickListener(OnClickListener listener) {
        this.mOnClickListener = listener;
    }

    public interface OnClickListener {

        void onClick();

        //长按
        void onLongClick(Bitmap bitmap);
    }



}
